//
//  YZLoopView.m
//  kuaiyi
//
//  Created by jacoy on 16/5/5.
//
//

#import "YZLoopView.h"
#import "YZLoopViewCell.h"
#import <Masonry.h>
#import "YZWeakTimeTarget.h"
@interface YZLoopView ()<UICollectionViewDataSource,UICollectionViewDelegate>
@property (nonatomic,weak)UICollectionView *collectionView;
@property (nonatomic,weak)UIPageControl *pageControl;
@property (nonatomic,strong)NSArray *imageArr;
@property (nonatomic,strong)NSTimer *timer;
@end

@implementation YZLoopView



-(instancetype)initWithCoder:(NSCoder *)aDecoder{
    if (self = [super initWithCoder:aDecoder]) {
        UIImage *image1 = [UIImage imageNamed:@"loop1"];
        UIImage *image2 = [UIImage imageNamed:@"loop2"];
        UIImage *image3 = [UIImage imageNamed:@"loop3"];


        self.imageArr = @[image1,image2,image3];
        
        [self prepareUI];
    }
    return self;
}

-(void)prepareUI{

    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    
    
    
    UICollectionView *collectionView=[[UICollectionView alloc]initWithFrame:self.bounds collectionViewLayout:layout];
    [collectionView registerClass:[YZLoopViewCell class] forCellWithReuseIdentifier:@"loopCell"];
    //设置数据源和代理
    collectionView.delegate=self;
    collectionView.dataSource=self;
    collectionView.backgroundColor=[UIColor redColor];
    [self addSubview:collectionView];

    
    layout.minimumInteritemSpacing=0;
    layout.minimumLineSpacing=0;
    layout.scrollDirection=UICollectionViewScrollDirectionHorizontal;

    
    self.collectionView=collectionView;
    self.collectionView.pagingEnabled=YES;
    self.collectionView.showsHorizontalScrollIndicator=NO;
    self.collectionView.bounces=NO;
    
    //分页控制器
    UIPageControl *pageControl=[[UIPageControl alloc]init];
    self.pageControl=pageControl;
    pageControl.numberOfPages = self.imageArr.count;
    pageControl.currentPageIndicatorTintColor=[UIColor redColor];
    pageControl.pageIndicatorTintColor=[UIColor grayColor];
    //如果只有一页,则隐藏分页指示器
    pageControl.hidesForSinglePage=YES;
    [self addSubview:pageControl];
    
    
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self);

        
    }];
    [self.pageControl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self);
        make.bottom.equalTo(self).offset(-10);
    }];
  
    layout.itemSize=CGSizeMake([UIScreen mainScreen].bounds.size.width, self.collectionView.bounds.size.height);

    [self addTimer];
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.imageArr.count*3;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    YZLoopViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"loopCell" forIndexPath:indexPath];
    cell.image=self.imageArr[indexPath.item%self.imageArr.count];
    return cell;
}

#pragma mark - scrollView的代理方法
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    NSInteger page=scrollView.contentOffset.x/scrollView.bounds.size.width;
    //    NSLog(@"page=%zd",page);
    if (page==0||page==([self.collectionView numberOfItemsInSection:0]-1)) {
        page=self.imageArr.count-((page==0)?0:1);
        //默默滚动到指定位置
        self.collectionView.contentOffset=CGPointMake(page*scrollView.bounds.size.width, 0);
    }
    //设置标题

    self.pageControl.currentPage=page%self.imageArr.count;
}
-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    [self addTimer];
}
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    [self removeTimer];
}
-(void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView{
    [self scrollViewDidEndDecelerating:scrollView];
}
#pragma mark - 定时器相关方法
-(void)addTimer{
    self.timer=[YZWeakTimeTarget scheduledTimerWithTimeInterval:2 target:self selector:@selector(nextImage) userInfo:nil repeats:YES];
    [[NSRunLoop currentRunLoop]addTimer:self.timer forMode:NSRunLoopCommonModes];
}
-(void)removeTimer{
    [self.timer invalidate];
    self.timer=nil;
}

-(void)nextImage{
    //获得当前显示的页号
    NSInteger page=self.collectionView.contentOffset.x/self.collectionView.bounds.size.width;
    CGFloat offsetX=(page+1)*self.collectionView.bounds.size.width;
    [self.collectionView setContentOffset:CGPointMake(offsetX, 0) animated:YES];
}



@end
